package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.MessageData;

public class MessageListAdapter extends BaseAdapter {

	private Context mContext;
	List<MessageData> mList;
	private LayoutInflater mInflater;

	public MessageListAdapter(Context ctx) {
		mContext = ctx;
		mInflater = (LayoutInflater) ctx
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mList = new ArrayList<MessageData>();
	}

	public void setData(List<MessageData> list){
		mList = new ArrayList<MessageData>();
		mList = list;
	}
	
	@Override
	public int getCount() {
		if (null == mList)
			return 0;
		else
			return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int index, View convertView, ViewGroup parent) {
		GridHolder holder = null;
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.message_list_item, null);   
			holder = new GridHolder();
			holder.headIV = (ImageView)convertView.findViewById(R.id.msg_head_iv);
			holder.numberTV =  (TextView)convertView.findViewById(R.id.msg_number_tv);
			holder.nameTV =  (TextView)convertView.findViewById(R.id.msg_name_tv);
			holder.lastMsgTV = (TextView) convertView.findViewById(R.id.msg_last_msg_tv);
			holder.dateTV = (TextView) convertView.findViewById(R.id.msg_date_tv);
			convertView.setTag(holder); 
		}else{
			 holder = (GridHolder) convertView.getTag();   
		}
		MessageData  data = mList.get(index);
		if (data != null) {
//			Bitmap bitmap = Utils.getHttpBitmap(data.getImageUrl());
//			holder.imageIV.setImageBitmap(bitmap);
			if(data.getNumber() > 0){
				holder.numberTV.setText(data.getNumber()+"");
				holder.numberTV.setVisibility(View.VISIBLE);
			}else{
				holder.numberTV.setVisibility(View.INVISIBLE);
			}
			holder.nameTV.setText(data.getName());
			holder.lastMsgTV.setText(data.getLastMsg());
			holder.dateTV.setText(data.getDate());
			
		}
		
		return convertView;
	}

	public class GridHolder {

		private ImageView headIV;
		private TextView numberTV;
		public TextView nameTV;
		public TextView lastMsgTV;
		public TextView dateTV;
		public ImageView getHeadIV() {
			return headIV;
		}
		public void setHeadIV(ImageView headIV) {
			this.headIV = headIV;
		}
		public TextView getNumberTV() {
			return numberTV;
		}
		public void setNumberTV(TextView numberTV) {
			this.numberTV = numberTV;
		}
		public TextView getNameTV() {
			return nameTV;
		}
		public void setNameTV(TextView nameTV) {
			this.nameTV = nameTV;
		}
		public TextView getLastMsgTV() {
			return lastMsgTV;
		}
		public void setLastMsgTV(TextView lastMsgTV) {
			this.lastMsgTV = lastMsgTV;
		}
		public TextView getDateTV() {
			return dateTV;
		}
		public void setDateTV(TextView dateTV) {
			this.dateTV = dateTV;
		}
		
	}
}
